package com.ml.controller;

import com.ml.dao.DkDao;
import com.ml.dao.TJUserDao;
import com.ml.dao.impl.DkDaoImpl;
import com.ml.dao.impl.TJUserDaoImpl;
import com.ml.model.*;
import com.ml.service.*;
import com.ml.util.UtilGoErrorPage;
import com.ml.util.UtilProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Controller
@RequestMapping(value = "tj")
public class TJController {
    @Autowired
    private SystemService systemService;
    @Autowired
    private MLUserService mLUserService;
    @Autowired
    private KqAccessService kqAccessService;
    @Autowired
    private KqDataService kqDataService;
    @Autowired
    private MLlogService mLlogService;

    /**
     * 跳转到工时页面
     */
    @RequestMapping({"/wk"})
    public String list(HttpServletRequest request) {


        Object sessionUser = request.getSession().getAttribute("mluser");
        if (sessionUser == null) {
            request.setAttribute("msg", "暂无权限");
            return "index.jsp";
        }
        MLUser user1 = (MLUser) sessionUser;
        if (user1 == null || user1.getId() == null) {
            request.setAttribute("msg", "暂无权限");
            return "index.jsp";
        }

        String searchUser = request.getParameter("searchuser");//要查询的人的名字
        String flagAdmin = UtilProperties.getConfig("flag.admin");

        Object flag = request.getSession().getAttribute("flag");
        TJUserDao ud = new TJUserDaoImpl();
        DkDao dk = new DkDaoImpl();
        //判断同步考情的权限
        String user = user1.getLastname();
        if ((flag != null && flag.toString().equals(flagAdmin))) {//有搜索权限的

            Object sessionUsers = request.getSession().getAttribute("users");
            if(sessionUsers==null){
                List users = ud.getAllUser();
                request.getSession().setAttribute("users", users);
            }
            if (searchUser != null && !searchUser.equals("")) {
                user = searchUser;//有搜索权限的
            }
        } else {//没有搜索权限的，只能查看自己的
            //user = searchUser ;
        }

        List<Dk> list = new ArrayList<>();
        String searchdate = request.getParameter("searchdate");
        String month = "";
        String year = "";
        if(StringUtils.hasText(searchdate)){
            year = searchdate.split("-")[0];
            month = searchdate.split("-")[1];
        }
        Calendar date = Calendar.getInstance();//通过Calendar获取年月
        if (month == null || "".equals(month)) {//没有传递参数默认查询当月的
            int m = date.get(Calendar.MONTH) + 1;
            month = m + "";
        }
        if (year == null || "".equals(year)) {
            year = String.valueOf(date.get(Calendar.YEAR));
        }
        //开始查询
        //按照名字查询
        //结束时间现在默认查询为一个月的
        int m2 = Integer.valueOf(month) + 1;//下个月的
        String stratdate = year + "/" + month; // access 支持这种格式的时间 2019/6/01 06:59:59
        String enddate = year + "/" + (Integer.valueOf(month) + 1);
        list = dk.getDkRecord(stratdate, enddate, user);

        request.setAttribute("month", month);//设置默认查询为当月
        request.setAttribute("year", year);//设置默认查询为当年
        request.setAttribute("list", list);//最终的查询结果
        request.setAttribute("searchUser", searchUser);//查询的人


        return "wk.jsp";
    }

    /**
     * 返回一个json对象
     */
    @RequestMapping({"/test"})
    @ResponseBody
    public AjaxMsg getById(HttpServletRequest request) {
        return AjaxMsg.returnAjaxMsg();
    }

    /**
     * 跳转到工时页面
     */
    @RequestMapping({"/kqtb"})
    public String kqtb(HttpServletRequest request) {
        Object sessionUser = request.getSession().getAttribute("mluser");
        if (sessionUser == null) {
            return UtilGoErrorPage.goError(request,"暂无权限");
        }
        MLUser user1 = (MLUser) sessionUser;
        if (user1 == null || user1.getId() == null) {
            return UtilGoErrorPage.goError(request,"暂无权限");
        }
        return "kqtb.jsp";
    }

}
